package com.fc.test.controller.gen;

import com.fc.test.model.auto.Question;
import com.fc.test.service.ExamQuestionService;
import com.fc.test.service.QuestionItemService;
import com.fc.test.service.QuestionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.PageInfo;
import com.fc.test.common.base.BaseController;
import com.fc.test.common.domain.AjaxResult;
import com.fc.test.model.auto.ExamPaper;
import com.fc.test.model.custom.TableSplitResult;
import com.fc.test.model.custom.Tablepar;
import com.fc.test.model.custom.TitleVo;
import com.fc.test.service.ExamPaperService;
import io.swagger.annotations.Api;

import java.util.List;

@Controller
@RequestMapping("ExamPaperController")
@Api(value = "")
public class ExamPaperController extends BaseController{
	
	private String prefix = "gen/examPaper";
	@Autowired
	private ExamPaperService examPaperService;
	@Autowired
	private QuestionService questionService;
	@Autowired
	private ExamQuestionService examQuestionService;
	@Autowired
	private QuestionItemService questionItemService;
	@GetMapping("view")
	@RequiresPermissions("gen:examPaper:view")
    public String view(ModelMap model)
    {	
		String str="";
		setTitle(model, new TitleVo("列表", str+"管理", true,"欢迎进入"+str+"页面", true, false));
        return prefix + "/list";
    }

	@GetMapping("ok")
	@RequiresPermissions("gen:examPaper:list")
	public String ok(String id, ModelMap mmap)
	{
		ExamPaper examPaper=examPaperService.selectByPrimaryKey(id);
		List<Question> qs=examQuestionService.listBypaperId(id);
		for (Question q:qs
			 ) {
			q.setQts(questionItemService.listByQuestionId(q.getId()));
		}
		examPaper.setQs(qs);
		 mmap.put("ExamPaper", examPaper);

		return prefix + "/ok";
	}

	//@Log(title = "集合查询", action = "111")
	@PostMapping("list")
	@RequiresPermissions("gen:examPaper:list")
	@ResponseBody
	public Object list(Tablepar tablepar,String searchTxt){
		PageInfo<ExamPaper> page=examPaperService.list(tablepar,searchTxt) ; 
		TableSplitResult<ExamPaper> result=new TableSplitResult<ExamPaper>(page.getPageNum(), page.getTotal(), page.getList()); 
		return  result;
	}
	
	/**
     * 新增
     */

    @GetMapping("/add")
    public String add(ModelMap modelMap)
    {
        return prefix + "/add";
    }
	
	//@Log(title = "新增", action = "111")
	@PostMapping("add")
	@RequiresPermissions("gen:examPaper:add")
	@ResponseBody
	public AjaxResult add(ExamPaper examPaper){
		int b=examPaperService.insertSelective(examPaper);
		if(b>0){
			return success();
		}else{
			return error();
		}
	}

	/**
	 * 删除用户
	 * @param ids
	 * @return
	 */
	//@Log(title = "删除", action = "111")
	@PostMapping("remove")
	@RequiresPermissions("gen:examPaper:remove")
	@ResponseBody
	public AjaxResult remove(String ids){
		int b=examPaperService.deleteByPrimaryKey(ids);
		if(b>0){
			return success();
		}else{
			return error();
		}
	}
	
	/**
	 * 检查用户
	 * @param tsysUser
	 * @return
	 */
	@PostMapping("checkNameUnique")
	@ResponseBody
	public int checkNameUnique(ExamPaper examPaper){
		int b=examPaperService.checkNameUnique(examPaper);
		if(b>0){
			return 1;
		}else{
			return 0;
		}
	}
	
	
	/**
	 * 修改跳转
	 * @param id
	 * @param mmap
	 * @return
	 */
	@GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("ExamPaper", examPaperService.selectByPrimaryKey(id));

        return prefix + "/edit";
    }
	
	/**
     * 修改保存
     */
    //@Log(title = "修改", action = "111")
    @RequiresPermissions("gen:examPaper:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ExamPaper record)
    {
        return toAjax(examPaperService.updateByPrimaryKeySelective(record));
    }

    
    

	
}
